package com.boe.csb.core.service;

import com.boe.csb.core.common.status.AdminUserRole;
import com.boe.csb.core.dao.EnvironmentFlagCriteria;
import com.boe.csb.core.dao.EnvironmentFlagMapper;
import com.boe.csb.core.dao.RoleFuncCriteria;
import com.boe.csb.core.dao.RoleFuncMapper;
import com.boe.csb.core.entity.po.BoeUser;
import com.boe.csb.core.entity.po.EnvironmentFlag;
import com.boe.csb.core.entity.po.RoleFunc;
import com.boe.csb.core.service.user.BoeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by tianxiang.luo on 16/11/30.
 */
@Service(value = "roleMapService")
@Scope("singleton")
public class RoleMapService {


    @Autowired
    private RoleFuncMapper roleFuncMapper;
    @Autowired
    private EnvironmentFlagMapper environmentFlagMapper;
    @Autowired
    private BoeUserService boeUserService;

    //是否初始化过
    private static boolean init = false;
    private static Date dataModifiedTime = new Date();
    private static Map<String, List<String>> roleMap = new ConcurrentHashMap<>();
    private static List<String> allUrlList = new ArrayList<>();

    /**
     * 获取权限
     *
     * @param roleKey
     * @return
     */
    public List<String> getRoleInfo(String roleKey) {
        //如果表更新过,则map需要重新填充数据
        if (mapNeedUpdate()) {
            EnvironmentFlag flag = new EnvironmentFlag();
            flag.setGmtModified(new Date());
            EnvironmentFlagCriteria criteria = new EnvironmentFlagCriteria();
            criteria.createCriteria().andDataEqualTo("role_func");
            //将数据更新为最新数据
            environmentFlagMapper.updateByExampleSelective(flag, criteria);
            refreshDataFromDB();
            return roleMap.get(roleKey);
        } else {
            return roleMap.get(roleKey);
        }
    }


    /**
     * 取出所有需要被拦截的url
     */
    public List<String> getFuncListByRole(String role) {
        if(StringUtils.isEmpty(role)) return null;
        String[] roles = role.split(",");
        List<String> funcList = new ArrayList<>();
        for (String r : roles) {
            if(StringUtils.isEmpty(r))continue;
            List<String> roleInfo = getRoleInfo(r);
            for (String s : roleInfo) {
                funcList.add(s);
            }
        }
        return funcList;
    }


    private Map<String, List<String>> refreshDataFromDB() {
        for (AdminUserRole adminUserRole : AdminUserRole.values()) {
            RoleFuncCriteria criteria = new RoleFuncCriteria();
            criteria.createCriteria().andRoleEqualTo(adminUserRole.getRole());
            List<RoleFunc> roleFuncList = roleFuncMapper.selectByExample(criteria);
            List<String> funcList = new ArrayList<>();
            for (RoleFunc roleFunc : roleFuncList) {
                funcList.add(roleFunc.getUrl());
            }
            roleMap.put(adminUserRole.getRole(), funcList);
        }
        dataModifiedTime = new Date();
        //如果>0则表示数据修改过
        init = true;
        return roleMap;
    }


    private boolean mapNeedUpdate() {
        //如果数据没有初始化过,则需要从数据库更新
        if (!init) return true;
        //如果数据修改过
        EnvironmentFlagCriteria criteria = new EnvironmentFlagCriteria();
        criteria.createCriteria().andDataEqualTo("role_func").andGmtModifiedGreaterThan(dataModifiedTime);
        //如果>0则表示数据修改过
        init = true;
        return environmentFlagMapper.countByExample(criteria) > 0;
    }


    public static List<String> getAllUrlList() {
        return allUrlList;
    }

    public static void setAllUrlList(List<String> allUrlList) {
        RoleMapService.allUrlList = allUrlList;
    }

}
